package socket3;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private Socket socket;

    public  Client() {
        System.out.println("正在连接服务器");
        try {
            socket=new Socket("localhost",8088);
            System.out.println("与服务器建立连接");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void start(){
        try {
            OutputStream out=socket.getOutputStream();
            OutputStreamWriter osw=new OutputStreamWriter(out);
            BufferedWriter bw=new BufferedWriter(osw);
            PrintWriter   pw=new PrintWriter(bw);
            //首先要求用户输入一个昵称
            Scanner sc=new Scanner(System.in);
            while (true){
                System.out.println("请输入昵称");
                String nickname=sc.nextLine();
                if(nickname.trim().length()!=0){
                    pw.println(nickname);
                    break;
                }
                System.out.println("昵称不能为空");

            }
            System.out.println("请输入聊天内容,或输入886退出");
            while (true){
                String meg=sc.nextLine();

                if ("886".equals(meg)){
                    break;
                }
                pw.println(meg);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        Client client=new Client();
        client.start();
    }
}
